package com.wsoft.bpm.controller;

import cn.hutool.core.util.StrUtil;
import com.wsoft.bpm.dto.RunProcessDTO;
import com.wsoft.bpm.query.run.AddSignQuery;
import com.wsoft.bpm.query.run.AgreeQuery;
import com.wsoft.bpm.query.run.NoticeQuery;
import com.wsoft.bpm.service.IActNoticeService;
import com.wsoft.bpm.service.RunProcessService;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zhonglj
 * @date 2024-09-19 16:05
 **/
@RestController
@Tag(name = "工作流运行时管理")
@RequestMapping("/bpm/run")
public class RunProcessController {

    @Resource
    private RunProcessService runProcessService;
    @Resource
    private IActNoticeService noticeService;

    @Operation(summary = "流程启动", description = "流程启动")
    @PostMapping("/startProcess")
    public ResponseResult startProcess(@RequestBody RunProcessDTO dto) {
        runProcessService.startProcess(dto.getProcessDefinitionKey(), dto.getTableName(), dto.getBusinessKey(), null, dto.getMap());
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "流程同意", description = "流程同意")
    @PostMapping("/agree")
    public ResponseResult agree(@RequestBody AgreeQuery query) {
        if (StrUtil.isBlank(query.getComment())) {
            query.setComment("同意");
        }
        runProcessService.agree(query);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "拾取", description = "拾取")
    @GetMapping("/claim")
    public ResponseResult claim(@Parameter(description = "任务id") String taskId) {
        runProcessService.claim(taskId);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "取消拾取", description = "取消拾取")
    @GetMapping("/unClaim")
    public ResponseResult unClaim(@Parameter(description = "任务id") String taskId) {
        runProcessService.unClaim(taskId);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "拒绝", description = "拒绝")
    @GetMapping("/refuse")
    public ResponseResult refuse(@Parameter(description = "任务id") String taskId,
                                 @Parameter(description = "拒绝意见") String comment) {
        if (StrUtil.isBlank(comment)) {
            comment = "拒绝";
        }
        runProcessService.refuse(taskId, comment, true);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "转签", description = "设置节点负责人")
    @GetMapping("/setAssign")
    public ResponseResult setAssign(@Parameter(description = "任务id") String taskId,
                                    @Parameter(description = "负责人") String assignee) {
        runProcessService.setAssign(taskId, assignee);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "退回到上一个人", description = "退回到上一个人")
    @GetMapping("/backOne")
    public ResponseResult backOne(@Parameter(description = "任务id") String taskId,
                                  @Parameter(description = "意见") String comment) {
        if (StrUtil.isBlank(comment)) {
            comment = "退回到上一个人";
        }
        runProcessService.backOne(taskId, comment);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "退回到指定活动", description = "退回到指定活动")
    @GetMapping("/backToAct")
    public ResponseResult backToAct(@Parameter(description = "任务id") String taskId,
                                    @Parameter(description = "意见") String comment,
                                    @Parameter(description = "指定活动id") String actId) {
        if (StrUtil.isBlank(comment)) {
            comment = "退回到指定活动";
        }
        runProcessService.backToAct(taskId, comment, actId);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "退回到提交人", description = "退回到提交人")
    @GetMapping("/back")
    public ResponseResult rollback(@Parameter(description = "任务id") String taskId,
                                   @Parameter(description = "意见") String comment) {
        if (StrUtil.isBlank(comment)) {
            comment = "退回到提交人";
        }
        runProcessService.rollback(taskId, comment);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "驳回", description = "驳回之后不允许再次提交")
    @GetMapping("/reject")
    public ResponseResult reject(@Parameter(description = "任务id") String taskId,
                                 @Parameter(description = "意见") String comment) {
        if (StrUtil.isBlank(comment)) {
            comment = "驳回";
        }
        runProcessService.reject(taskId, comment);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "流程加签", description = "流程加签")
    @PostMapping("/addSign")
    public ResponseResult addSign(@RequestBody AddSignQuery signQuery) {
        runProcessService.addSign(signQuery);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "知会", description = "知会")
    @PostMapping("/notice")
    public ResponseResult notice(@RequestBody NoticeQuery noticeQuery) {
        noticeService.notice(noticeQuery.getTaskId(), noticeQuery.getAssigns());
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "知会已读", description = "知会已读")
    @GetMapping("/notice/read")
    public ResponseResult readNotice(@Parameter(description = "知会id") Long id) {
        noticeService.readNotice(id);
        return ResponseResult.success("[sys.msg.success]");
    }

    @Operation(summary = "结束流程", description = "结束流程")
    @GetMapping("/stopProcess")
    public ResponseResult stopProcess(@Parameter(description = "流程实例id") String processInstanceId) {
        runProcessService.stopProcess(processInstanceId);
        return ResponseResult.success("[sys.msg.success]");
    }
}
